﻿using Infrastructure;
using Infrastructure.Extensions;
using IP2Region.Ex.Models;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.DataCollection.Model;
using ZR.DataCollection.OperationCore.OperationEnum;
using ZR.DataCollection.OperationCore.OperationHelper;
using ZR.DataCollection.SingleParameter;
using ZR.Model.Models.ProductionControl;
using ZR.Service.Business;
using ZR.Service.Business.IBusinessService;
using ZR.Service.IService.ProductionControl;
using ZR.Service.Service.ProductionControl;
using ZR.ServiceCore.Signalr;

namespace ZR.DataCollection.OperationCore.OperationMode.OP20
{
    /// <summary>
    /// 请求加工
    /// </summary>
    public class ProcessOP20 : OperationInfoBase
    {
        //1上工位OK；
        //2上工位不存在；
        //3上工位NG；
        //4本工位OK；
        //5本工位NG；
        //6首件/点检/标样；
        //7连续NG；
        //8累计NG；
        //10=产品空条码；
        private Int16 collectStatus = 1;

        public override event Action<string> Event_Notification;

        private readonly IOrderDetailInfoService _orderDetailInfoService = App.GetRequiredService<IOrderDetailInfoService>();
        private readonly IWorkstationConfigService _workstationConfigService = App.GetRequiredService<IWorkstationConfigService>();
        private readonly IStationVerificationConfigService _stationVerificationConfigService = App.GetRequiredService<IStationVerificationConfigService>();
        private readonly IStationWorkInfoService _stationWorkInfoService = App.GetRequiredService<IStationWorkInfoService>();
        private readonly IHubContext<MessageHub> _hubContext = App.GetRequiredService<IHubContext<MessageHub>>();

        public ProcessOP20(int operationType, string stationType) : base(operationType, stationType)
        {
        }

        public override void WorkDetail(ImpParameter imp)
        {
            DataDock? dataDock = imp.objectParam as DataDock;
            var keyValuePairs = CurrentCruxPoint(dataDock.LineCode, dataDock.ProductType, dataDock.ItemNo);
            SpotHandle(imp, keyValuePairs);
        }

        public override void SpotHandle(ImpParameter parameter, Dictionary<int, List<OperationPointConfig>> SpotGroup)
        {
            foreach (var SpotInfo in SpotGroup)
            {
                switch (SpotInfo.Key)
                {
                    case (int)DataOperateEnum.Read:
                        ReadFunction(parameter, SpotInfo.Value);
                        break;

                    case (int)DataOperateEnum.Write:
                        SpotInfo.Value.ForEach(x =>
                        {
                            if (collectStatus.Equals(1))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP20Allow", "允许加工");
                            }
                            else if (collectStatus.Equals(2))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP20Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(3))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP20Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(4))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP20Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(5))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP20Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(6))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP20Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(7))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP20Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(8))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP20Allow", "不允许加工");
                            }
                            else if (collectStatus.Equals(10))
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, collectStatus);
                                _hubContext.Clients.All.SendAsync($"OP20Allow", "不允许加工");
                            }
                            else
                            {
                                parameter.PLCDetail.WriteValue(x.ItemNo, Convert.ToInt16(0));
                            }
                        });
                        break;

                    case (int)DataOperateEnum.Verify:
                        break;

                    default:
                        break;
                }
            }
        }

        private async void ReadFunction(ImpParameter parameter, List<OperationPointConfig> OperationPointList)
        {
            DataDock dataDock = parameter.objectParam as DataDock;
            for (int i = 0; i < OperationPointList.Count; i = i + 3)
            {
                //产品
                string? productId = parameter.PLCDetail.ReadValue(OperationPointList[i].ItemNo).ToString();
                //转盘号
                string? turntableNo = parameter.PLCDetail.ReadValue(OperationPointList[i + 1].ItemNo).ToString();
                _ = _hubContext.Clients.All.SendAsync($"OP20BarCode", productId);
                if (string.IsNullOrWhiteSpace(productId))//产品空条码
                {
                    collectStatus = 10;
                    return;
                }
                StationWorkInfo stationWorkInfo = null;
                bool sve = _workstationConfigService.QueryInfo(dataDock?.LineCode, dataDock?.StationCode).StationVerifyEnabled.Equals(1);
                if (sve)
                {
                    string upperStation = _stationVerificationConfigService.UpperWorkstation(dataDock.LineCode, dataDock.StationCode);
                    stationWorkInfo = _stationWorkInfoService.QueryInfo(dataDock.LineCode, upperStation, productId);
                    if (stationWorkInfo.IsNullOrZero())//上工位信息
                    {
                        collectStatus = 2;
                        return;
                    }
                    if (stationWorkInfo.WorkResult.Equals("NG"))//上工位结果为NG
                    {
                        collectStatus = 3;
                        return;
                    }
                }
                OrderDetailInfo orderDetail = await _orderDetailInfoService.QueryByClauseAsync(qbc => qbc.ProductId.Equals(productId));
                var workResult = await OverStationStart(dataDock, orderDetail, productId, turntableNo);
                if (workResult < 0)
                {
                    collectStatus = 5;
                }
            }
        }
    }
}